package dao;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import beans.Goods;
import beans.Package;

import utils.HibernateUtil;




public class GoodsDao {

	private SessionFactory sessionFactory;
	Transaction tr = null;

	public GoodsDao() {
		sessionFactory = HibernateUtil.getSessionFactory();
	}
	
	public Goods getGoodsById(int goodsId) {
		Session session = sessionFactory.openSession();
		tr = session.beginTransaction();
		Goods goods = (Goods) session.get(Goods.class, goodsId);
		tr.commit();
		session.close();
		return goods;
	}
	
	public Goods resetLatestGoods(int terminalId, int boxId) {
		TerminalDao terminalDao = new TerminalDao();
		Session session = sessionFactory.openSession();
		tr = session.beginTransaction();
		List<Goods> goods = null;
		Object []status = {"4","5"};
		Criteria c = session.createCriteria(Goods.class);
		c.add(Restrictions.eq("terminal",terminalDao.getTerminalById(terminalId)));
		c.add(Restrictions.eq("boxNum", boxId));
		c.add(Restrictions.in("status", status));
		c.addOrder(Order.desc("storeTime"));
		goods = (ArrayList<Goods>) c.list();
		if (goods.iterator().hasNext()) {
			Goods g = goods.iterator().next();
			g.setStatus("3");
			g.setGetTime(null);
			g.setRemark("6");
			session.update(g);
			tr.commit();
			session.close();
			return g;
		} else {
			tr.commit();
			session.close();
			return null;
		}
	}

	/**
	 * 
	 * @param phoneNum
	 *            手机号码
	 * @return 历史物品信息
	 */

	@SuppressWarnings("unchecked")
	public ArrayList<Goods> getHistoryGoodsByPhone(String phoneNum) {

		Session session = sessionFactory.openSession();
		ArrayList<Goods> goods = null;

		tr = session.beginTransaction();

		Criteria c = session.createCriteria(Goods.class);
		c.add(Restrictions.eq("userMobileNum", phoneNum));
		c.add(Restrictions.eq("status", 4));
		c.addOrder(Order.desc("storeTime"));
		goods = (ArrayList<Goods>) c.list();
		tr.commit();
		session.close();
		return goods;

	}

	/**
	 * 查询用户物品
	 * 
	 * @param phoneNum
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public ArrayList<Goods> getByUserPhone(String phoneNum) {

		Session session = sessionFactory.openSession();
		ArrayList<Goods> goods = null;

		tr = session.beginTransaction();

		Criteria c = session.createCriteria(Goods.class);
		c.add(Restrictions.like("userMobileNum", "%" + phoneNum + "%"));
		c.addOrder(Order.desc("storeTime"));
		goods = (ArrayList<Goods>) c.list();
		tr.commit();
		session.close();
		return goods;

	}

	/**
	 * 查询投递员物品
	 * 
	 * @param phoneNum
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public ArrayList<Goods> getByCourierPhone(String phoneNum) {

		Session session = sessionFactory.openSession();
		ArrayList<Goods> goods = null;
		tr = session.beginTransaction();

		Criteria c = session.createCriteria(Goods.class);
		c.add(Restrictions.like("mobileNum", "%" + phoneNum + "%"));
		c.addOrder(Order.desc("getTime"));
		goods = (ArrayList<Goods>) c.list();

		tr.commit();
		session.close();
		return goods;

	}

	/**
	 * 获取该手机号码未取的物品
	 * 
	 */
	@SuppressWarnings("unchecked")
	public List<Goods> getUnTakenGoodsDetailByPhone(String phoneNum) {
		Session session = sessionFactory.openSession();
		tr = session.beginTransaction();
		Criteria c = session.createCriteria(Goods.class);
		c.add(Restrictions.eq("userMobileNum", phoneNum));
		c.add(Restrictions.eq("status", 3));
		c.addOrder(Order.desc("storeTime"));
		List<Goods> goods = c.list();
		tr.commit();
		session.close();
		return goods;
	}

	/**
	 * 根据单号返回物品信息
	 * 
	 * @param orderNo
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public ArrayList<Goods> getByOrderNum(String orderNo) {

		Session session = sessionFactory.openSession();
		ArrayList<Goods> goods = null;
		tr = session.beginTransaction();

		Criteria c = session.createCriteria(Goods.class);
		c.add(Restrictions.eq("orderNo", orderNo));
		c.addOrder(Order.asc("getTime"));
		goods = (ArrayList<Goods>) c.list();

		tr.commit();
		session.close();
		return goods;

	}

	/**
	 * 根据快递单号返回相应包裹（模糊查询）
	 * 
	 * @param parcelID
	 *            快递单号
	 * @return 快递包裹信息的集合
	 */

	@SuppressWarnings("unchecked")
	public ArrayList<Goods> getGoodsDetailByOrderNo(String orderNo) {

		Session session = sessionFactory.openSession();
		ArrayList<Goods> goods = null;
		tr = session.beginTransaction();

		Criteria c = session.createCriteria(Goods.class);
		c.add(Restrictions.like("orderNo", "%" + orderNo + "%"));
		c.addOrder(Order.asc("getTime"));
		goods = (ArrayList<Goods>) c.list();

		tr.commit();
		session.close();
		return goods;

	}
	/**
	 * 获取设备剩余物品列表
	 * @param terminalID
	 * @return
	 */
	public ArrayList<Goods> getUnTakenGoods(int terminalID) {
		
		TerminalDao terminalDao = new TerminalDao();
		Session session = sessionFactory.openSession();
		ArrayList<Goods> goods = null;
		Object[] status = {"3","8"};
		tr = session.beginTransaction();
		
		Criteria c = session.createCriteria(Goods.class);
		c.add(Restrictions.eq("terminal", terminalDao.getTerminalById(terminalID)));
		c.add(Restrictions.in("status", status));
		c.addOrder(Order.asc("boxNum"));
		goods = (ArrayList<Goods>) c.list();
		
		tr.commit();
		session.close();
		return goods;
		
	}
	
	/**
	 * 获取设备箱格历史物品
	 * @param terminalID
	 * @param boxNum
	 * @return
	 */
	public ArrayList<Goods> getHistoryGoods(int terminalID, int boxNum) {
		
		TerminalDao terminalDao = new TerminalDao();
		Session session = sessionFactory.openSession();
		ArrayList<Goods> goods = null;
		tr = session.beginTransaction();
		
		Criteria c = session.createCriteria(Goods.class);
		c.add(Restrictions.eq("terminal", terminalDao.getTerminalById(terminalID)));
		c.add(Restrictions.eq("boxNum", boxNum));
		c.addOrder(Order.desc("storeTime"));
		goods = (ArrayList<Goods>) c.list();
		
		tr.commit();
		session.close();
		return goods;
		
	}
	/**
	 * 获取存入物品详情
	 * @param terminalID
	 * @param boxNum
	 * @return
	 */
	public ArrayList<Goods> getGoodsDetailsByBoxNum(int terminalID, int boxNum) {
		
		TerminalDao terminalDao = new TerminalDao();
		Session session = sessionFactory.openSession();
		ArrayList<Goods> goods = null;
		Object[] status = {"0","1","2","3","5","8"};
		tr = session.beginTransaction();
		
		Criteria c = session.createCriteria(Goods.class);
		c.add(Restrictions.eq("terminal", terminalDao.getTerminalById(terminalID)));
		c.add(Restrictions.eq("boxNum", boxNum));
		c.add(Restrictions.in("status", status));
		c.addOrder(Order.desc("storeTime"));
		goods = (ArrayList<Goods>) c.list();
		
		tr.commit();
		session.close();
		return goods;
		
	}

	/**
	 * 修改物品信息
	 * 
	 * @param p
	 * @return
	 */
	public Goods editGoods(Goods p) {
		Session session = sessionFactory.openSession();
		tr = session.beginTransaction();
		session.update(p);
		tr.commit();
		session.close();
		return p;
	}

}